package com.tyk.bookstore.back.storage.manager.excel.config;

import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.exception.CustomException;
import com.tyk.bookstore.back.common.core.model.result.CommonResultEnum;
import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.platform.service.AreaApiService;
import com.tyk.bookstore.back.product.service.BookApiService;
import com.tyk.bookstore.back.storage.manager.excel.model.StockExcelVo;
import com.tyk.bookstore.back.storage.manager.excel.model.WarehouseExcelVo;
import com.tyk.bookstore.back.storage.model.entity.StockDo;
import com.tyk.bookstore.back.storage.model.entity.WarehouseDo;
import com.tyk.bookstore.back.storage.model.result.StockResultEnum;
import com.tyk.bookstore.back.storage.model.result.WarehouseResultEnum;
import com.tyk.bookstore.back.storage.repository.StockRepository;
import com.tyk.bookstore.back.storage.repository.WarehouseRepository;
import com.tyk.bookstore.back.system.service.UserDetailApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author tyk
 * @description StorageExcelConfig
 */
@Configuration
@RequiredArgsConstructor
public class StorageExcelConfig {

    private final StockRepository stockRepository;
    private final WarehouseRepository warehouseRepository;
    private final BookApiService bookApiService;
    private final AreaApiService areaApiService;
    private final UserDetailApiService userDetailApiService;

    @Bean
    public ExcelManager<StockExcelVo> stockExcelManager() {
        return new ExcelManager<>() {
            @Override
            public void checkRow(StockExcelVo data) {
                // 仓库存在
                AssertUtil.isTrue(
                        warehouseRepository.exist1Eq(
                                WarehouseDo::getId, data.getWarehouseId(), WarehouseDo.class
                        ),
                        WarehouseResultEnum.WAREHOUSE_NOT_FOUND
                );

                // 书籍存在
                bookApiService.assertExistById(Long.valueOf(data.getBookId()));

                // 库存不存在
                AssertUtil.isFalse(
                        stockRepository.exist2Eq(
                                StockDo::getWarehouseId, data.getWarehouseId(),
                                StockDo::getBookId, data.getBookId(),
                                StockDo.class
                        ),
                        StockResultEnum.STOCK_ALREADY_EXISTS
                );
            }

            @Override
            public void handleSaveException(MultipartFile file, Exception e) {
                throw new CustomException(CommonResultEnum.FILE_OR_DATA_FORMAT_ERROR);
            }
        };
    }

    @Bean
    public ExcelManager<WarehouseExcelVo> warehouseExcelManager() {
        return new ExcelManager<>() {
            @Override
            public void checkRow(WarehouseExcelVo data) {
                // 校验区域Code和用户ID
                areaApiService.assertLastAreaByCode(data.getAreaCode());
                userDetailApiService.assertExistById(Long.valueOf(data.getUserId()));

                // 不存在位置相同且名称相同的仓库
                AssertUtil.isFalse(
                        warehouseRepository.exist2Eq(
                                WarehouseDo::getAreaCode, data.getAreaCode(),
                                WarehouseDo::getName, data.getName(),
                                WarehouseDo.class
                        ),
                        WarehouseResultEnum.WAREHOUSE_ALREADY_EXISTS
                );
            }

            @Override
            public void handleSaveException(MultipartFile file, Exception e) {
                throw new CustomException(CommonResultEnum.FILE_OR_DATA_FORMAT_ERROR);
            }
        };
    }
}
